﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SqlOperate;
using nspLable.Function;

/// <summary>
/// 诚招代理的操作类
/// </summary>
namespace Agent
{
    public class RSincereAgent : ISincereAgent
    {
        public static string ConnectionString = ConfigurationManager.ConnectionStrings["sqlData"].ConnectionString;
        #region TSincereAgent 成员
        /// <summary>
        /// 添加诚招代理加盟
        /// </summary>
        /// <param name="eui">诚招代理加盟对象</param>
        /// <returns></returns>
        public int Insert(TSincereAgent eui, out int intReturn)
        {
            SqlParameter[] commandParameters = new SqlParameter[]
            {                
                	new SqlParameter("@Return", DbType.Int32),
                   	new SqlParameter("@UserID", eui.UserID),
					new SqlParameter("@Name", eui.Name),
					new SqlParameter("@AgentType", eui.AgentType),
					new SqlParameter("@Title", eui.Title),
					new SqlParameter("@Validity", eui.Validity),
					new SqlParameter("@Brand", eui.Brand),
					new SqlParameter("@Investment", eui.Investment),
					new SqlParameter("@Amount", eui.Amount),
					new SqlParameter("@InitialFee", eui.InitialFee),
					new SqlParameter("@Require", eui.Require),
					new SqlParameter("@Cradle", eui.Cradle),
					new SqlParameter("@Payback", eui.Payback),
					new SqlParameter("@Period", eui.Period),
					new SqlParameter("@Concession", eui.Concession),
					new SqlParameter("@Contract", eui.Contract),
					new SqlParameter("@Count", eui.Count),
					new SqlParameter("@BusinessDate", eui.BusinessDate),
					new SqlParameter("@isMoreBrand", eui.isMoreBrand),
					new SqlParameter("@BusinessMode", eui.BusinessMode),
					new SqlParameter("@Developmental", eui.Developmental),
					new SqlParameter("@Detail", eui.Detail),
					new SqlParameter("@Picture", eui.Picture),
                    new SqlParameter("@SinType",eui.SinType),
                    new SqlParameter("@Obligate", eui.Obligate)
               

             };
            intReturn = 0;
            //commandParameters[1].Direction = ParameterDirection.Output;
            int ReRows = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "SP_W_SincereAgent_Add", commandParameters);
            if (ReRows >= 0)
                intReturn = (int)commandParameters[0].Value;
            return ReRows;
        }

        /// <summary>
        /// 修改诚招代理加盟
        /// </summary>
        /// <param name="eui">诚招代理加盟对象<</param>
        /// <returns></returns>
        public int Update(TSincereAgent eui)
        {
            SqlParameter[] commandParameters = new SqlParameter[]
            {
                    new SqlParameter("ID",eui.ID),
                    new SqlParameter("@Name", eui.Name),
					new SqlParameter("@AgentType", eui.AgentType),
					new SqlParameter("@Title", eui.Title),
					new SqlParameter("@Validity", eui.Validity),
					new SqlParameter("@Brand", eui.Brand),
					new SqlParameter("@Investment", eui.Investment),
					new SqlParameter("@Amount", eui.Amount),
					new SqlParameter("@InitialFee", eui.InitialFee),
					new SqlParameter("@Require", eui.Require),
					new SqlParameter("@Cradle", eui.Cradle),
					new SqlParameter("@Payback", eui.Payback),
					new SqlParameter("@Period", eui.Period),
					new SqlParameter("@Concession", eui.Concession),
					new SqlParameter("@Contract", eui.Contract),
					new SqlParameter("@Count", eui.Count),
					new SqlParameter("@BusinessDate", eui.BusinessDate),
					new SqlParameter("@isMoreBrand", eui.isMoreBrand),
					new SqlParameter("@BusinessMode", eui.BusinessMode),
					new SqlParameter("@Developmental", eui.Developmental),
					new SqlParameter("@Detail", eui.Detail),
					new SqlParameter("@Picture", eui.Picture),
                    new SqlParameter("@SinType",eui.SinType),
                    new SqlParameter("@Obligate", eui.Obligate)

             };
            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "SP_W_SincereAgent_Update", commandParameters);
        }

        /// <summary>
        /// 修改审核信息
        /// </summary>
        /// <param name="eui"></param>
        /// <returns></returns>
        public int UpdateVerify(TSincereAgent eui)
        {
            SqlParameter[] commandParameters = new SqlParameter[]
            {
                new SqlParameter("@ID", eui.ID),
                new SqlParameter("@Verify", eui.Verify)
               
             };
            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "SP_W_SincereAgent_UpdateVerify", commandParameters);
        }

        /// <summary>
        /// 修改推荐信息
        /// </summary>
        /// <param name="eui"></param>
        /// <returns></returns>
        public int UpdateRecommend(TSincereAgent eui)
        {
            SqlParameter[] commandParameters = new SqlParameter[]
            {
                new SqlParameter("@ID", eui.ID),
                new SqlParameter("@Recommend", eui.Recommend)
               
             };
            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "SP_W_SincereAgent_UpdateRecommend", commandParameters);
        }

        /// <summary>
        /// 根据ID删除诚招代理加盟
        /// </summary>
        /// <param name="ID">供应ID</param>
        /// <returns></returns>
        public int Delete(int ID)
        {
            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@strwhere", "where ID in (" + ID + ")"), 
                new SqlParameter("@strTableName", "W_SincereAgent") 
            };
            return SqlHelper.DeleteByWhere("SP_DeleteByWhere", param);

        }
        /// <summary>
        /// 根据AgentType 行业类别删除诚招代理加盟
        /// </summary>
        /// <param name="ID">供应ID</param>
        /// <returns></returns>
        public int Delete(string ID)
        {
            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@strwhere", "where AgentType in (" + ID + ")"), 
                new SqlParameter("@strTableName", "W_SincereAgent") 
            };
            return SqlHelper.DeleteByWhere("SP_DeleteByWhere", param);

        }

        /// <summary>
        /// //根据标题名称和代理加盟所属类目取得诚招代理加盟信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable(string strWhere)
        {
            SqlParameter[] param = new SqlParameter[] 
            { 
                new SqlParameter("@strWhere",strWhere),
                new SqlParameter("@strTableName","W_SincereAgent"), 
                new SqlParameter("@strOrder", " order by AddDate desc,ID desc")
            };
            return SqlHelper.ExecuteTable(CommandType.StoredProcedure, "SP_SelectByWhere", param);

        }

        /// <summary>
        /// 根据诚招代理加盟类型得到诚招代理加盟
        /// </summary>
        /// <param name="ParamID"></param>
        /// <returns></returns>
        public DataTable GetDataTable(TSincereAgent us)
        {
            string str = "where 1=1";
            if (us.UserID != 0)
                str += " and UserID=" + us.UserID;
            if (us.Verify != 0)
            {
                if (us.Verify == 4)
                    str += " and (datediff(day,AddDate,getdate())>Validity)";
                else
                    str += " and Verify=" + us.Verify;
            }
            else
                str += " and Verify=" + 1;

            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@strwhere", str),
                new SqlParameter("@strtableName", "W_SincereAgent"), 
                new SqlParameter("@strOrder", " order by AddDate desc,ID desc") 
            };
            return SqlHelper.ExecuteTable(CommandType.StoredProcedure, "SP_SelectByWhere", param);
        }

        /// <summary>
        /// 根据ID得到诚招代理加盟每一条信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public TSincereAgent GateItem(int ID)
        {
            SqlParameter[] param = new SqlParameter[] 
            { 
                new SqlParameter("@strWhere", "where ID=" +ID), 
                new SqlParameter("@strTableName", "W_SincereAgent"), 
                new SqlParameter("@strOrder", "") 
            };
            TSincereAgent info = new TSincereAgent();
            using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "SP_SelectByWhere", param))
            {
                if (!reader.Read())
                {
                    return info;
                }
                if (reader["ID"].ToString() != "")
                {
                    info.ID = int.Parse(reader["ID"].ToString());
                }
                if (reader["UserID"].ToString() != "")
                {
                    info.UserID = int.Parse(reader["UserID"].ToString());
                }
                info.Name = reader["Name"].ToString();
                if (reader["AgentType"].ToString() != "")
                {
                    info.AgentType = int.Parse(reader["AgentType"].ToString());
                }
                info.Title = (reader["Title"].ToString());
                info.Validity = reader["Validity"].ToString();
                info.Brand = reader["Brand"].ToString();
                info.Investment = reader["Investment"].ToString();

                if (reader["Amount"].ToString() != "")
                {
                    info.Amount = Convert.ToDecimal(reader["Amount"].ToString());
                }
                if (reader["InitialFee"].ToString() != "")
                {
                    info.InitialFee = Convert.ToDecimal(reader["InitialFee"].ToString());
                }
                info.Require = reader["Require"].ToString();
                info.Cradle = reader["Cradle"].ToString();
                if (reader["Payback"].ToString() != "")
                {
                    info.Payback = Convert.ToDecimal(reader["Payback"].ToString());
                }
                if (reader["Period"].ToString() != "")
                {
                    info.Period = Convert.ToDecimal(reader["Period"].ToString());
                }
                if (reader["Concession"].ToString() != "")
                {
                    info.Concession = Convert.ToDecimal(reader["Concession"].ToString());
                }
                if (reader["Contract"].ToString() != "")
                {
                    info.Contract = Convert.ToDecimal(reader["Contract"].ToString());
                }
                if (reader["Count"].ToString() != "")
                {
                    info.Count = Convert.ToInt32(reader["Count"].ToString());
                }
                if (reader["BusinessDate"].ToString() != "")
                {
                    info.BusinessDate = Convert.ToInt32(reader["BusinessDate"].ToString());
                }
                if (reader["isMoreBrand"].ToString() != "")
                {
                    info.isMoreBrand = Convert.ToBoolean(reader["isMoreBrand"].ToString());
                }
                info.BusinessMode = reader["BusinessMode"].ToString();
                info.Developmental = reader["Developmental"].ToString();
                info.Detail = reader["Detail"].ToString();
                info.Picture = (reader["Picture"].ToString());
                if (reader["Verify"].ToString() != "")
                {
                    info.Verify = Convert.ToInt32(reader["Verify"].ToString());
                }
                if (reader["Recommend"].ToString() != "")
                {
                    info.Recommend = Convert.ToBoolean(reader["Recommend"].ToString());
                }
                if (reader["AddDate"].ToString() != "")
                {
                    info.AddDate = Convert.ToDateTime(reader["AddDate"].ToString());
                }
                info.Obligate = reader["Obligate"].ToString();
                if (reader["SinType"].ToString() != "")
                {
                    info.SinType = int.Parse(reader["SinType"].ToString());
                }
            }
            return info;
        }

        /// <summary>
        /// 根据父级ID得到代理加盟子类别每一条信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetSonType(int ParentID)
        {
            DataTable dt = new DataTable();
            string Sql = "SELECT * FROM [W_AgentType] WHERE [ParentID]=" + ParentID;
            dt = SqlHelper.ExecuteSqlQuery(Sql);
            return dt;
        }

        /// <summary>
        /// 获得所有的招商信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllInfo()
        {
            string Sql = "SELECT * FROM [W_SincereAgent]";
            DataTable dt=new DataTable();
            dt = SqlHelper.ExecuteSqlQuery(Sql);
            return dt;
        }

        public DataTable GetSonlist(int PageSize, int PageIndex, string strWhere)
        {
            //string strWhere = " U_UserInfo.Verify=1 AND U_UserInfo.GradeID=U_VipGrade.ID and datediff(hh,U_UserInfo.EndDate,getdate())>0";
            int pagesize = 10;
            string strTable = " [W_SincereAgent] as a left join U_UserInfo as b on a.UserID=b.UserID left join W_AgentType as c on a.AgentType=c.ID ";
            int OrderType = 1;
            int PageID = 1;  //x
            string OrderField = " U_VipGrade.Sort desc, C_Supply.AddDate ";
            string strSelect = "  a.*,b.CompanyName,b.Province,b.City,c.*,b.LoginName";
            string strFldName = "AddDate";
            //int PageSize = 10;
            //int PageIndex = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"]);
            if (PageIndex < 1) PageIndex = 1;
            int Count = 0;

            DataTable Lists = Pagination.GetDataTable(strWhere, strTable, OrderType, strFldName, strSelect, pagesize, PageID, 0, "a.id");
            return Lists;
        }
        public DataTable GetSonlist(string strWhere, string orderStr)
        {
            string strsql = "SELECT a.*,b.CompanyName,b.Province,b.City,c.*,b.LoginName FROM [W_SincereAgent] as a left join U_UserInfo as b on a.UserID=b.UserID left join W_AgentType as c on a.AgentType=c.ID   ";
            if (strWhere != "")
            {
                strsql += " where " + strWhere;
            }
            if (orderStr != "")
            {
                strsql += orderStr;
            }
            DataTable dt = SqlHelper.ExecuteSqlQuery(strsql);
            return dt;
        }
        /// <summary>
        /// 取得分类
        /// </summary>
        /// <returns></returns>
        public string GetType(string strType)
        {
            int tid = 0;
            if (HttpContext.Current.Request.QueryString["ID"] != null)
                tid = Convert.ToInt32(HttpContext.Current.Request.QueryString["ID"]);
            int pid = 0;
            if (tid != 0)
            {
                pid = tid;
            }
            DataTable dt = new Agent.RAgentType().GetDataTable(pid);
            string Restr = "<p class=\"ltnr2\">";
            foreach(DataRow dr in dt.Rows)
            {
                Restr += "<span><a class=\"l_666\" href=\"Trade-" + dr["id"].ToString() + Base.BasePage.Config.Rewrite + "\">" + dr["Name"].ToString() + "(" + dr["acount"] + ")</a></span>";
            }
            Restr += "</p>";
            return Restr;
        }
        //根据广告位获取广告信息
        public DataTable GetAD(int num, int adtypeID)
        {
            DataTable dt = SqlHelper.ExecuteSqlQuery("SELECT top " + num + " a.*,b.Ad_Width,b.Ad_Height FROM [W_Ad_MemberInfo] as a, [W_Ad_Table] as b WHERE a.T_ID=b.T_ID and a.T_ID=" + adtypeID);
            return dt;
        }
        public DataTable GetHotIndustry(int num)
        {
            DataTable dt = SqlHelper.ExecuteSqlQuery(" select top " + num + " a.ID,a.[Name],a.ParentID,a.Compositor,count(b.ID) as acount  from W_AgentType as a , dbo.W_SincereAgent as b where  a.ID=b.AgentType group by a.ID,a.[Name],a.ParentID,a.Compositor order by count(b.ID) desc");
            return dt;
        }
        public DataTable GetZDGQInfo(int num, string tabName, int isTop)
        {
            string sql = "select ";
            if (isTop == 1)
            {
                if (num > 0)
                {
                    sql += " top " + num + " ";
                }
                sql += " a.*,b.Province as area from " + tabName + " as a left join U_UserInfo as b on a.UserID=b.UserID where a.Verify=1 order by adddate desc ";
            }
            DataTable dt = SqlHelper.ExecuteSqlQuery(sql);
            return dt;
        }
        public string TypeLink(int id)
        {
            string titleStr = "";
            Agent.TAgentType Items = new Agent.RAgentType().GateItem(id);
         
            if (Items.ParentID != 0)
            {
                Agent.TAgentType SItems = new Agent.RAgentType().GateItem(Items.ParentID);
                titleStr += "<a title=\"\" href=\"" + Base.BasePage.Config.WebURL + "agent/Trade-" + SItems.ID.ToString() + Base.BasePage.Config.Rewrite + "\">" + SItems.Name + "</a>&nbsp;&gt;&nbsp;";
            }
            titleStr += Items.Name;
            return titleStr;
        }
        #endregion
    }
}
